<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['userName']))
	exit();

function searchABMRestaurants()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('idRestaurant, businessName, address, phone, email')
			->from('restaurant')
			->where("restaurant.state <> '".$stateErase."'");

	return $datatables->generate();
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
	
	if(!isset($_REQUEST["idRestaurant"])){
		$strSQL = "SELECT businessName FROM restaurant WHERE businessName = '".$_REQUEST["restaurantBusinessName"]."' AND state <> '$stateErase'";
		$rsUsers = $DB->Execute($strSQL);		

		if(!$rsUsers->EOF)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->msg = $a_languages['Common.ErrorDuplicado'];
			return json_encode($arrJSON);
		}
			
		$strSQL = "INSERT INTO restaurant ( ";
		$strSQL .= "	businessName, ";
		$strSQL .= "	otherName, ";
		$strSQL .= "	address, ";
		$strSQL .= "	zipCode, ";
		$strSQL .= "	phone, ";
		$strSQL .= "	fax, ";
		$strSQL .= "	otherPhone, ";
		$strSQL .= "	email, ";
		$strSQL .= "	taxId, ";
		$strSQL .= "	taxType, ";
		$strSQL .= "	idCountry, ";
		$strSQL .= "	state ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantBusinessName"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantOtherName"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantsAddress"], "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["restaurantsZipCode"]."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantsPhone"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantsFax"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["restaurantsOtherPhone"], "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["restaurantsEMail"]."', ";
		$strSQL .= "	'".$taxId."', ";
		$strSQL .= "	".$taxType.", ";
		$strSQL .= "	".$idDefaultCountry.", ";
		$strSQL .= "	'".$stateAdd."'";
		$strSQL .= "	)";
	}
	else{
		$strSQL = "UPDATE restaurant SET ";
		$strSQL .= "	businessName = '".mb_strtoupper($_REQUEST["restaurantBusinessName"], "UTF-8")."', ";
		$strSQL .= "	otherName = '".mb_strtoupper($_REQUEST["restaurantOtherName"], "UTF-8")."', ";
		$strSQL .= "	address = '".mb_strtoupper($_REQUEST["restaurantsAddress"], "UTF-8")."', ";
		$strSQL .= "	zipCode = '".$_REQUEST["restaurantsZipCode"]."', ";
		$strSQL .= "	phone = '".mb_strtoupper($_REQUEST["restaurantsPhone"], "UTF-8")."', ";
		$strSQL .= "	fax = '".mb_strtoupper($_REQUEST["restaurantsFax"], "UTF-8")."', ";
		$strSQL .= "	otherPhone = '".mb_strtoupper($_REQUEST["restaurantsOtherPhone"], "UTF-8")."', ";
		$strSQL .= "	email = '".$_REQUEST["restaurantsEMail"]."', ";
		$strSQL .= "	state = '".$stateModify."',";
		$strSQL .= "	taxId = '".$taxId."',";
		$strSQL .= "	taxType = ".$taxType.",";
		$strSQL .= "	idCountry = ".$idDefaultCountry;
		$strSQL .= " WHERE idRestaurant = ". $_REQUEST["idRestaurant"];
	}
	
	$DB->Execute($strSQL);
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE restaurant SET state='$stateErase' where idRestaurant=" . $_REQUEST["idRestaurant"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkEliminar'];
	}	
	return json_encode($arrJSON);
}

function editRestaurant()
{
	require("includes/constants.php");
		
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsRestaurants = $DB->Execute("SELECT * FROM restaurant WHERE state <> '$stateErase' and idRestaurant=" . $_REQUEST["idRestaurant"]); // execute query
						
	if(!$rsRestaurants->EOF){
		$joRestaurants->data['restaurantBusinessName'] = $rsRestaurants->fields("businessName");
		$joRestaurants->data['restaurantOtherName'] = $rsRestaurants->fields("otherName");
		$joRestaurants->data['restaurantsAddress'] = $rsRestaurants->fields("address");
		$joRestaurants->data['restaurantsZipCode'] = $rsRestaurants->fields("zipCode");
		$joRestaurants->data['restaurantsPhone'] = $rsRestaurants->fields("phone");
		$joRestaurants->data['restaurantsFax'] = $rsRestaurants->fields("fax");
		$joRestaurants->data['restaurantsOtherPhone'] = $rsRestaurants->fields("otherPhone");
		$joRestaurants->data['restaurantsEMail'] = $rsRestaurants->fields("email");
		
		return json_encode($joRestaurants->data);
	}
	else
		return json_encode('');
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editRestaurant();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMRestaurants();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;	
}
?>
